/**
 * JAVACC DEMO 1.0
 */
package com.apache.rpc.service.impl;

import com.apache.api.api.ApiService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * description:
 *
 * @author Hou Dayu 创建时间：2016-11-4
 */
public class InterfaceRegisterServiceImpl implements ApiService {

    private Map<String, PluginConnector> plugins;

    private Logger log = LoggerFactory.getLogger(InterfaceRegisterServiceImpl.class);

    //@HystrixCommand(fallbackMethod = "error")
    public ResultEntity doService(ParamsVo vo) throws ServiceException {
        ResultEntity result = new ResultEntity();
        result.setResult("false");
        if (Validator.isEmpty(plugins.get(vo.getMethodKey()))) {
            result.setMessage("方法[" + vo.getMethodKey() + "]没有找到");
        } else {
            try {
                return (ResultEntity) plugins.get(vo.getMethodKey()).execute(vo);
            } catch (Exception e) {
                log.error("异常",e);
                throw new ServiceException("执行方法[" + vo.getMethodKey() + "]", e);
            }
        }
        return result;
    }

    public ResultEntity error(ParamsVo vo) {
        ResultEntity result = new ResultEntity();
        result.setResult("false");
        result.setMessage("系统繁忙");
        return result;
    }

    public void setPlugins(Map<String, PluginConnector> plugins) {
        this.plugins = plugins;
    }

}
